package com.hjxy.controller;

import com.github.pagehelper.PageInfo;
import com.hjxy.pojo.Student;
import com.hjxy.pojo.Teacher;
import com.hjxy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ty
 * @createDate: 2020/9/28
 * @version: 1.0
 */
@Controller
@RequestMapping("/Tian")
public class StudentController {

    @Autowired
    private StudentService studentService;

//    @RequestMapping("/test1")
//    @ResponseBody
//    public String test(Model model){
//        Model model1 = model.addAttribute("msg", "整合成功");
//        List<Student> students = studentService.queryAllStudent();
//        System.out.println(students);
//        System.out.println(model1);
//        return "welcome";
//    }

    //跳转到学生管理页面
    @RequestMapping("/Long")
    public String Long(){
        System.out.println("跳转页面");
        return "student";
    }

    //查询所有学生
    @RequestMapping("/queryStudent")
    @ResponseBody
    public Map<String, Object> queryStudents(){
        System.out.println("到查询了");
        List<Student> students = studentService.queryAllStudent();
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","欢迎登录");
        result.put("students",students);
        //加了一打印
        System.out.println(result);
        return result;
    }

    //跳转到增加页面
    @RequestMapping("/addStudent")
    public String add(){

        System.out.println("到增加学生信息这里来了");
        return "AddStudent";
    }

//增加学生的代码

    @RequestMapping("/J")
    @ResponseBody
    public Map<String,Object> addStudent(Student student){
//        这里的是调用StudentServiceImpl里面的方法
        System.out.println("--------------");
        System.out.println("student===>"+student);

        //
        int i =studentService.AddStudent(student);
        Map<String,Object> result = new HashMap<>();
        result.put("code",1);
        result.put("msg","成功");
        result.put("student",student);
        System.out.println(result+"<============");
        return result;

    }

    //删除学生信息
    @RequestMapping("/deletedStudent")
    @ResponseBody
    public Map<String, Object> deletedStudent(Integer userId){

        System.out.println("userId====>"+userId);
        studentService.deleteStudent(userId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","删除");
        return result;
    }

    //对学生进行分页
//    @RequestMapping("/FY")
//    @ResponseBody
//    public Map<String, Object> Fy(Integer currentPage, Integer pageSize){
//        System.out.println("-------");
//        PageInfo<Student> pageInfo = studentService.paging(currentPage, pageSize);
//        System.out.println("============");
//        Map<String, Object> map = new HashMap<>();
//        map.put("code",0);
//        map.put("msg","成功获取用户数据");
//        //返回查询出来的分页数据
//        map.put("userList",pageInfo.getList());
//        //返回当前总页数
//        map.put("totalPage",pageInfo.getPages());
//        System.out.println("map======>"+  map);
//        return map;
//    }

    //修改学生信息
    @RequestMapping("/Update")
    @ResponseBody
    public Map<String,Object> edit1(Integer userId){
        Map<String, Object> result = new HashMap<>();
        Student student = studentService.edit3(userId);
        if (student == null){
            result.put("code",-1);
            result.put("message","false");

        }else {
            result.put("code",0);
            result.put("msg","nice");
            result.put("student",student);
        }
        return result;

    }


    @RequestMapping("/Update1")
    @ResponseBody
    public Map<String, Object> edit2(Student student){
        Map<String, Object> result = new HashMap<>();
        boolean modified;
        if(student.getUserId() != 0) {
            modified = studentService.edit1(student);
        }else{
            modified = studentService.edit2(student);
        }
        if(!modified){
            result.put("code", -1);
            result.put("message", "编辑用户数据失败");
        }else{
            System.out.println(result+"<-----------2");
            result.put("code", 0);
            result.put("message", "编辑用户数据成功");
        }
        System.out.println(result+"<-----------3");
        return result;
    }

}
